package za.ac.wits.elen7045.persistence.datasource.sqlite;

import java.sql.ResultSet;
import java.sql.SQLException;
import za.ac.wits.elen7045.persistence.datasource.DataSourceRowEntry;
import za.ac.wits.elen7045.persistence.model.CreditCardProviderStatement;
import za.ac.wits.elen7045.persistence.model.CreditCardType;
import za.ac.wits.elen7045.persistence.util.AccountTableConstants;
import za.ac.wits.elen7045.persistence.util.CreditCardProviderStatementTableConstants;
import za.ac.wits.elen7045.persistence.util.StatementTableConstants;

public class CreditCardProviderStatementSQLiteDataSource extends SQLiteDataSource<CreditCardProviderStatement> implements AccountTableConstants, StatementTableConstants, CreditCardProviderStatementTableConstants {

	@Override
	protected CreditCardProviderStatement parseCurrentRowToObject(ResultSet resultSet) throws SQLException {
		CreditCardProviderStatement statement = new CreditCardProviderStatement();
		/* Common Fields */
		statement.setAccountId(resultSet.getInt(ACCOUNT_ID));
		statement.setId(resultSet.getInt(STATEMENT_ID));
		statement.setStatementNumber(resultSet.getInt(STATEMENT_NUMBER));
		statement.setStatementDate(resultSet.getString(STATEMENT_DATE));
		statement.setAmountDue(resultSet.getInt(TOTAL_DUE));
		statement.setAmountDueDate(resultSet.getString(DUE_DATE));
		statement.setOpenBalance(resultSet.getInt(OPENING_BALANCE));
		statement.setCloseBalance(resultSet.getInt(CLOSING_BALANCE));
		statement.setPaymentReceived(resultSet.getInt(PAYMENT_RECEIVED));
		statement.setNewCharges(resultSet.getInt(NEW_CHARGES));
		statement.setDeductions(resultSet.getInt(DEDUCTIONS));
		statement.setDiscount(resultSet.getInt(DISCOUNT));
		statement.setVat(resultSet.getInt(VAT));
		/* Municipality-Specific Fields */
		statement.setCardType(CreditCardType.getTypeByName(resultSet.getString(CARD_TYPE)));
		statement.setInterestRate(resultSet.getDouble(INTEREST_RATE));
		statement.setCreditLimit(resultSet.getInt(CREDIT_LIMIT));
		statement.setCreditAvailable(resultSet.getInt(CREDIT_AVAILABLE));
		statement.setMinAmountDue(resultSet.getInt(MIN_AMOUNT_DUE));
		return statement;
	}

	@Override
	protected DataSourceRowEntry parseObjectToDataSourceRowEntry(CreditCardProviderStatement statement) throws SQLException {
		DataSourceRowEntry entry = new DataSourceRowEntry();
		/* Common Fields */
		entry.setValue(ACCOUNT_ID, statement.getAccountId());
		entry.setValue(STATEMENT_ID, statement.getId());
		entry.setValue(STATEMENT_NUMBER, statement.getStatementNumber());
		entry.setValue(STATEMENT_DATE, statement.getStatementDate());
		entry.setValue(TOTAL_DUE, statement.getAmountDue());
		entry.setValue(DUE_DATE, statement.getAmountDueDate());
		entry.setValue(OPENING_BALANCE, statement.getOpenBalance());
		entry.setValue(CLOSING_BALANCE, statement.getCloseBalance());
		entry.setValue(PAYMENT_RECEIVED, statement.getPaymentReceived());
		entry.setValue(NEW_CHARGES, statement.getNewCharges());
		entry.setValue(DEDUCTIONS, statement.getDeductions());
		entry.setValue(DISCOUNT, statement.getDiscount());
		entry.setValue(VAT, statement.getVat());
		/* Municipality-Specific Fields */
		entry.setValue(CARD_TYPE, statement.getCardType().getName());
		entry.setValue(INTEREST_RATE, statement.getInterestRate());
		entry.setValue(CREDIT_LIMIT, statement.getCreditLimit());
		entry.setValue(CREDIT_AVAILABLE, statement.getCreditAvailable());
		entry.setValue(MIN_AMOUNT_DUE, statement.getMinAmountDue());
		return entry;
	}

}
